package org.example.inhandler;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.embedded.EmbeddedChannel;

/**
 * @author hh
 * @since 2024/10/20
 */
public class Test {
    public static void main(String[] args) {
        InHandlerDemo inHandlerDemo = new InHandlerDemo();
        // 初始化处理器
        ChannelInitializer<EmbeddedChannel> i = new ChannelInitializer<EmbeddedChannel>() {
            @Override
            protected void initChannel(EmbeddedChannel embeddedChannel) throws Exception {
                embeddedChannel.pipeline().addLast(inHandlerDemo);
            }
        };
        // 创建嵌入式通道
        EmbeddedChannel channel = new EmbeddedChannel(i);
        ByteBuf buf = Unpooled.buffer();
        buf.writeInt(1);
        // 模拟入站，向嵌入式通道写入一个入站数据包
        channel.writeInbound(buf);
        channel.flush();
        // 模拟入站，再写一个入站数据包
        channel.writeInbound(buf);
        channel.flush();
        // 通道关闭
        channel.close();
    }
}
